
package com.ykdl.member.kid.adapters;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.PmBean;
import com.ykdl.member.kid.circle.LookImageActivity;
import com.ykdl.member.kid.image.ImageOptions;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.FileMetas;
import com.ykdl.member.kid.person.PersonCenterActivity;
import com.ykdl.member.kid.util.MyChatMsgEntity;
import com.ykdl.member.kid.widget.AsyncImageView;
/**
 * 聊天Adapter
 * @author chenshuai
 */
public class MyChatMsgViewAdapter extends BaseAdapter {
	private ImageOptions options=null ;
	private String TAG ;
	private int current_actor_id ;
	private BitmapUtils bitmapUtil ;
	public static interface IMsgViewType
	{
		int IMVT_COM_MSG = 0;
		int IMVT_TO_MSG = 1;
	}
	private ArrayList<PmBean>  list ;
	
    private List<MyChatMsgEntity> coll;

    private Context ctx;
    
    private LayoutInflater mInflater;

    public MyChatMsgViewAdapter(Context context,String tag) {
        ctx = context;
        TAG = tag ;
        mInflater = LayoutInflater.from(ctx);
        options = ImageOptionsFactory.getBigPortraitOptions(ctx);
        bitmapUtil = new BitmapUtils(context);
    }
    public void setList(ArrayList<PmBean>  list) {
		this.list = list;
		initData();
	}
    public void setActorid(int current_actor_id ) {
		this.current_actor_id = current_actor_id;
		initData();
	}
	public void initData() {
		List<MyChatMsgEntity> mDataArrays = new ArrayList<MyChatMsgEntity>();
		if (list!=null) {
			for (int i = 0; i < list.size(); i++) {
				MyChatMsgEntity entity = new MyChatMsgEntity();
				entity.setDate(list.get(i).getCreated_time());
				entity.setBean(list.get(i).getFrom_actor());
				if (list.get(i).getFrom_actor().getActor_id()!=current_actor_id) {
					entity.setMsgType(true);
				} else {
					entity.setMsgType(false);
				}
				if (list.get(i).getAttachment_type()==1) {
					entity.setHasImage(true);
					entity.setFile_metas(list.get(i).getAttachment_files());
				}else {
					entity.setHasImage(false);
				}

				entity.setText(list.get(i).getContent());
				mDataArrays.add(entity);
			}
		}
		coll = mDataArrays;

	}
    public int getCount() {
    	if (coll!=null) {
    		 return coll.size();
		}else {
			return 0;
		}
       
    }

    public Object getItem(int position) {
        return coll.get(position);
    }

    public long getItemId(int position) {
        return position;
    }
    


	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		MyChatMsgEntity entity = coll.get(position);
	 	
	 	if (entity.getMsgType())
	 	{
	 		return IMsgViewType.IMVT_COM_MSG;
	 	}else{
	 		return IMsgViewType.IMVT_TO_MSG;
	 	}
	 	
	}


	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}
	
	
    public View getView(int position, View convertView, ViewGroup parent) {
    	
    	MyChatMsgEntity entity = coll.get(position);
    	boolean isComMsg = entity.getMsgType();
    		
    	ViewHolder viewHolder = null;	
	    if (convertView == null)
	    {
	    	  if (isComMsg)
			  {
				  convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);
			  }else{
				  convertView = mInflater.inflate(R.layout.chatt_item_msg_text_right, null);
			  }

	    	  viewHolder = new ViewHolder();
			  viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
			  viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
			  viewHolder.isComMsg = isComMsg;
			  viewHolder.iv_userhead =  (ImageView) convertView.findViewById(R.id.iv_userhead);
			  viewHolder.user_grade = (TextView) convertView.findViewById(R.id.user_grade);
			  viewHolder.topic_picture1 =  (ImageView) convertView.findViewById(R.id.topic_picture1);
			  convertView.setTag(viewHolder);
	    }else{
	        viewHolder = (ViewHolder) convertView.getTag();
	    }
	    if (entity.getBean().getAvatar_file_meta()!=null) {
	    	String url = entity.getBean().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
	    	bitmapUtil.display(viewHolder.iv_userhead, url);
		}
	    viewHolder.iv_userhead.setTag(entity.getBean().getActor_id());
	    viewHolder.iv_userhead.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ctx, PersonCenterActivity.class);
				intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, (Integer)v.getTag());
				ctx.startActivity(intent);
			}
		});
	    viewHolder.tvUserName.setText(entity.getBean().getDisplay_name());
	    viewHolder.tvContent.setText(entity.getText());
	    viewHolder.user_grade .setText(entity.getBean().getLevel()+"");
	    if (entity.isHasImage()) {
	    	 viewHolder.topic_picture1.setVisibility(View.VISIBLE);
	    	doholderpicdata(viewHolder,
	    			entity.getFile_metas()) ;
		}else {
			 viewHolder.topic_picture1.setVisibility(View.GONE);
		}
	    return convertView;
    }
    public void doholderpicdata(ViewHolder holder,
			 final ArrayList<FileMetas> arrayList) {
			String url = arrayList.get(0).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture1, url);
		holder.topic_picture1
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						lookbigimage(arrayList, 0);
					}
				});
	}

    static class ViewHolder { 
        public TextView tvUserName,tvSendTime,tvContent,user_grade;
        public ImageView  iv_userhead,topic_picture1;
        LinearLayout topic_user_pic ;
        public boolean isComMsg = true;
    }
    public void lookbigimage(ArrayList<FileMetas> file_meta,int position) {
		Intent intent = new Intent(ctx, LookImageActivity.class);
		intent.putExtra("list",  (Serializable)file_meta);
		intent.putExtra("position",  position);
		ctx.startActivity(intent);
	}

}
